'''
Created on Aug 20, 2019

@author: yc
'''
import httpInteface as hf
import pygame
from  pygame.locals import  *
from sys import exit

redColor = pygame.Color(0,0,0)
whiteColor = pygame.Color(255,255,255)

def main():
    #定义一个变量来控制速度
    fpsClock=pygame.time.Clock()
    pygame.init() #初始化游戏并创建一个屏幕对象
    wf = hf.getWindowInfo()
    print("wid=",wf.get("space_width"), ",  heig=", wf.get("space_height"))
    screen = pygame.display.set_mode((wf.get("space_width"), wf.get("space_height")))
    pygame.display.set_caption('hello, pygame')
    screen.fill(whiteColor)
    
    # 画一个迷宫
    for e in hf.getMazeInfo():
        pygame.draw.line(screen, e[0], e[1], e[2], e[3])
    
    pygame.display.flip() #让最近回执的屏幕可见
    cx = -1
    cy = -1
    while True: #开始游戏主循环
        for event in pygame.event.get(): #监视键盘和鼠标事件
            if event.type == pygame.QUIT:
                exit()
        #擦除之前的车子
        if cx != -1:
            pygame.draw.rect(screen,whiteColor,Rect(cx,cy,10,10))     
            pygame.display.flip()
        
        #画车子
        carL = hf.getCarInfo().get("location")
        cx = carL[0]
        cy = carL[1]
        print("cx=",cx, ", cy=", cy)
        pygame.draw.rect(screen,redColor,Rect(cx,cy,10,10))     
        pygame.display.flip()
        
        #控制游戏速度
        fpsClock.tick(1)
                

if __name__ == '__main__':
    main()
